package com.roncoo.education.db.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.roncoo.education.api.request.NewsRequest;
import com.roncoo.education.db.dao.NewsDao;
import com.roncoo.education.db.entity.News;
import com.roncoo.education.util.FileService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NewsService {

    @Resource
    NewsDao newsDao;

    @Resource
    FileService fileService;

    public Map<String,String> saveNews(Long adminId, NewsRequest req){
        News news = BeanUtil.copyProperties(req,News.class);
        Long nid = 0l;
        if(news.getId()!=null){
            News oldNews = newsDao.findById(news.getId());
            if(oldNews!=null){
                if(news.getImgUrl()!=null && !news.getImgUrl().equals(oldNews.getImgUrl())){
                    //删除旧图文件
                    if(StrUtil.isNotBlank(oldNews.getImgUrl())) {
                        fileService.deleteFileByUrl(oldNews.getImgUrl());
                    }
                }
            }
            nid = news.getId();
            news.setAdminId(adminId);
            news.setPublishTime(new Date());
            newsDao.updateById(news);
        }else{
            news.setAdminId(adminId);
            news.setPublishTime(new Date());
            news.setDelFlag(1);
            newsDao.insert(news);
            nid = news.getId();
        }
        Map<String,String> ret = new HashMap<>();
        ret.put("isError", "success");
        ret.put("id", String.valueOf(nid));
        ret.put("code", "1");
        return ret;
    }

    public void setDeleteNews(Long id){
        News news = newsDao.findById(id);
        if(news!=null){
            News update = new News();
            update.setId(id);
            update.setDelFlag(0);
            newsDao.updateById(update);
        }
    }

    public List<News> findByBean(News news){
        return newsDao.findByBean(news);
    }

    public void newsViewsNumAdd(Long id,Integer views){
        News update = new News();
        update.setId(id);
        if(views==null){
            update.setViews(1);
        }else {
            update.setViews(views + 1);
        }
        newsDao.updateById(update);
    }

    public News getNewById(Long id){
        return newsDao.findById(id);
    }

    public PageInfo<News> getNewsPages(NewsRequest req){
        int pageNum = req.getPageNum();
        int pageSize = req.getCount();
        if (req.getPageNum() == null || req.getPageNum() <= 0 || req.getCount() == null) {
            pageNum = 1;
            pageSize = 20;
        }
        News query = new News();
        query.setTitle(req.getTitle());
        query.setCategory(req.getCategory());
        query.setDelFlag(1);
        if(StrUtil.isNotBlank(req.getOrderClause())) {
            query.setOrderClause(req.getOrderClause());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<News> newsList = newsDao.findByNewsByTitle(query);
        return new PageInfo<>(newsList);
    }
}
